package com.cityfood.controller;

import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cityfood.common.pojo.CityFoodResult;
import com.cityfood.common.utils.CheckUserUtils;
import com.cityfood.pojo.CfUser;
import com.cityfood.pojo.CfAdminUser;
import com.cityfood.pojo.CfSessions;
import com.cityfood.service.UserService;
import com.cityfood.service.AdminUserService;
import com.cityfood.service.SessionService;

@Controller
public class LoginController {

	@Autowired
	private UserService userService;
	
	//session管理
	@Autowired
	private SessionService sessionService;
	
	//注入adminUserService接口实现类
	@Autowired
	private AdminUserService adminUserService;
	
	@SuppressWarnings("deprecation")
	@ResponseBody
	@RequestMapping("/user/login")
	public CityFoodResult doLogin(HttpServletRequest req,@RequestParam(value = "code", required = false) String code) {

		CityFoodResult cityFoodResult = new CityFoodResult();

		HttpSession session = req.getSession();
		String sessionId = session.getId();
		// post请求获取的SessionAndopenId
		JSONObject SessionKeyOpenId = CheckUserUtils.getSessionKeyOrOpenId(code);
		// openid
		String openid = SessionKeyOpenId.getString("openid");
		// sessionKey
		String sessionKey = SessionKeyOpenId.getString("session_key");
		
		int currentUid = 0; 

		CfUser user = userService.findUserByOpenid(openid);
		
		if (user == null ) {   //首次登陆
			user = new CfUser();
			user.setOpenid(openid);
			user.setUsername("微信用户");  //微信不使用
			user.setPassword("123456");	 //微信不使用
			user.setPhone("");	         //未绑定前手机号为空
			user.setGender("男");
			// 添加一个用户
			userService.insertUser(user);
			CfUser newUser = userService.findUserByOpenid(openid);
			
			
			if (newUser != null ) {
				// 用户登陆成功,放入session
				CfSessions map = new CfSessions();
				map.setUid(newUser.getId());
				map.setSessionid(sessionId);
				map.setSessionkey(sessionKey);
				map.setLoginTime(new Date().getHours());
				sessionService.createSessionMap(map);
				currentUid = newUser.getId();
				session.setAttribute("user", newUser);
			}else{
				cityFoodResult.setCode(201);
				cityFoodResult.setMsg("用户登陆失败");
				cityFoodResult.setData("");   
				return cityFoodResult;
			}
		} else {   //非首次登陆
			CfSessions map2 = sessionService.getSessionMap(user.getId());
			map2.setSessionid(sessionId);
			map2.setSessionkey(sessionKey);
			map2.setLoginTime(new Date().getHours());
			currentUid = user.getId();
			sessionService.refreshSessionMap(map2);
			session.setAttribute("user", user);
			
		}
		CfUser currentUser = userService.getUserByUserId(currentUid);
		Boolean isBindPhone =  !currentUser.getPhone().isEmpty();
		HashMap<Object, Object> res = new HashMap<Object, Object>();
		res.put("sessinId", sessionId);   //sessionId返回给前端
		res.put("isBindPhone", isBindPhone);  //是否绑定手机
		res.put("uid", currentUid);   //当前用户UID
		CfAdminUser adminUser =  adminUserService.getAdminUserByPhone(currentUser.getPhone());
		if(adminUser != null){
			res.put("role", 1);  //1表示配送员
			res.put("suid", adminUser.getId()); //登陆控制台用sid
		}else{
			res.put("role", 0);  //0表示用户
			res.put("suid", 0);  //0表示用户 
		}
		if (isBindPhone) {
			res.put("phone", currentUser.getPhone());   //当前用户手机号
		}
		
		cityFoodResult.setCode(200);
		cityFoodResult.setMsg("用户登陆成功");
		cityFoodResult.setData(res);

		return cityFoodResult;
	}
	
}
